package top.chmin.project.cloud.common.config.auto;

// field import auto
// #bind.field.uiPath#

import cn.hutool.core.bean.BeanUtil;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.chmin.project.cloud.common.entity.Page;

import java.util.List;
import java.util.stream.Stream;



/**
 * chmin
 * 2023-12-27 14:44:36
 * cloud-builder
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class ConfigService {

    @Resource
    ConfigPlugin plugin;

    @Resource
    ConfigDao configDao;

// field labelDao auto

    public int save(ConfigSave.Param param) {
        plugin.checkSave(param);
        Config config = BeanUtil.toBean(param, Config.class);
        plugin.beforeSave(config);
        int result = configDao.save(config);
        plugin.afterSave(param, config, result);
        return result;
    }

    public int updateById(ConfigUpdateById.Param param) {
        plugin.checkUpdateById(param);
        Config config = BeanUtil.toBean(param, Config.class);
        plugin.beforeUpdateById(config);
        int result = configDao.updateById(config);
        plugin.afterUpdateById(param, config, result);
        return result;
    }

    public int deleteById(ConfigDeleteById.Param param) {
        plugin.checkDeleteById(param);
        int result = configDao.deleteById(param.getId());
        plugin.afterDeleteById(param, result);
        return result;
    }

    public ConfigSelectById.Result selectById(ConfigSelectById.Param param) {
        plugin.checkSelectById(param);
        ConfigFPC fpc = new ConfigFPC().id().eq(param.getId());
        plugin.beforeSelectById(param, fpc);
        ConfigSelectById.Result result = configDao.getFirst(fpc.toCondition(), ConfigSelectById.Result.class);
        plugin.afterSelectById(param, result);
        return result;
    }

    public Page<ConfigSelectPage.Result> selectPage(ConfigSelectPage.Param param) {
        plugin.checkSelectPage(param);
        ConfigFPC fpc = new ConfigFPC();
        fpc = param.isDesc() ? fpc.id().desc() : fpc.id().asc();
        plugin.beforeSelectPage(param, fpc);
        Page<ConfigSelectPage.Result> result = configDao.getPageByCondition(fpc.toCondition(), ConfigSelectPage.Result.class);
// field label auto
        plugin.afterSelectPage(param, result);
        return result;
    }

    // entity select auto
    public List<ConfigConfigCodeSelect.Result> selectConfigCode(ConfigConfigCodeSelect.Param param) {
        plugin.checkSelectConfigCode(param);
        ConfigFPC fpc = new ConfigFPC();
        plugin.beforeSelectConfigCode(param, fpc);
        Stream<ConfigConfigCodeSelect.Result> result = configDao.getStreamByCondition(fpc.toCondition(), ConfigConfigCodeSelect.Result.class)
                .peek(r -> {
                    r.setValue(r.getConfigCode());
                    r.setLabel(r.getConfigName());
                });
        List<ConfigConfigCodeSelect.Result> list = result.toList();
        plugin.afterSelectConfigCode(param, list);
        return list;
    }


// entity search auto

}
